package resourcespecification

import (
	"encoding/json"
	"fleetmanager/api/common/log"
	"fleetmanager/api/model/fleet"
	"fleetmanager/api/response"
	service "fleetmanager/api/service/fleet"
	"fleetmanager/api/validator"
	"fleetmanager/logger"
	"github.com/beego/beego/v2/server/web"
	"net/http"
)

type UpdateController struct {
	web.Controller
}

// UpdateResourceSpecification: 更新实例规格
func (c *UpdateController) UpdateResourceSpecification() {
	tLogger := log.GetTraceLogger(c.Ctx).WithField(logger.Stage, "update_resource_specification")
	r := fleet.UpdateResourceSpecificationRequest{}
	if err := json.Unmarshal(c.Ctx.Input.RequestBody, &r); err != nil {
		response.InputError(c.Ctx)
		tLogger.WithField(logger.Error, err.Error()).Error("read update resource specification request body error")
		return
	}
	if err := validator.Validate(&r); err != nil {
		response.ParamsError(c.Ctx, err)
		tLogger.WithField(logger.Error, err.Error()).Error("parameters invalid")
		return
	}
	s := service.NewFleetService(c.Ctx, tLogger)
	_, _, e := s.UpdateResourceSpecification(&r)
	if e != nil {
		response.ServiceError(c.Ctx, e)
		tLogger.WithField(logger.Error, e.Error()).Error("update resource specification error")
		return
	}
	response.Success(c.Ctx, http.StatusNoContent, nil)
}
